//
//  PluPlu.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-12.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "PluPlu.h"
#import "Singleton.h"
#import "Common.h"

@implementation PluPlu
@synthesize pmCode;
@synthesize psCode;
@synthesize ppCode;
@synthesize ppTemp;
@synthesize ppStatus;

@synthesize ppColor;
@synthesize ppPosition;
@synthesize ppHelpCode;
@synthesize ppDescripts;
@synthesize ppImage;
@synthesize ppRecommended;
@synthesize ppSeqNormal;
@synthesize ppSeqCommend;
@synthesize ppType;
@synthesize ppFood;
@synthesize ppCigarete;
@synthesize ppDrink;
@synthesize ppPluPkgs;

@synthesize ppPluPrices;
@synthesize ppMyOrderDishes;

//=========================================================== 
// - (id)init
//
//=========================================================== 
- (id)init
{
    self = [super init];
    if (self) {
        ppPluPkgs = [NSMutableArray new];
        ppPluPrices = [NSMutableArray new];
        ppMyOrderDishes = [NSMutableArray new];
    }
    return self;
}


-(void)insertNewMyOrderDish:(NSString *)numberString price:(NSString *)price units:(NSString *)units{
    NSString *updateSql = [NSString stringWithFormat:@"insert into MyOrderDishes(momLocalMenu,pmCode,psCode,ppCode,ppType,modDescripts,modNumber,modPrice,modUnits,modCook,modPriceUpdate,modTime) values('"] ;
    updateSql = [updateSql stringByAppendingFormat:@"%@','",AppSession.currentOrderMenu.momLocalMenu];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.pmCode];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.psCode];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppCode];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppType];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppDescripts];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",numberString];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",price];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",units];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",@" "];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",([price floatValue]==0 ? @"1":@"0")];
    updateSql = [updateSql stringByAppendingFormat:@"%@')",CurrentTime];
    [DataBaseAccess Update:updateSql];
    
    if ([self.ppType isEqualToString:@"3"]) {//如果点的是套餐，就把明细插入MyOrderPluPkgs表
        NSString *updateSql1 = [NSString stringWithFormat:@"insert into MyOrderPluPkgs(momLocalMenu, ppCode, pkgCode, pkgDescripts, pkgTemp, pkgNumber,pkgPrice,pkgCook,pkgUnit) select '%@',ppCode,pkgCode,pkgDescripts,pkgTemp,'1.00',pkgPrice,'',pkgUnit from PluPkg where ppCode = '%@'",AppSession.currentOrderMenu.momLocalMenu, self.ppCode];
        [DataBaseAccess Update:updateSql1];
    }
    
    [self updateMyOrderDishes];//更新属性
}
-(void)insertNewMyOrderPkg:(NSString *)numberString price:(NSString *)price units:(NSString *)units{
    NSString *updateSql = [NSString stringWithFormat:@"insert into MyOrderPluPkgs(momLocalMenu,ppCode,pkgCode, pkgDescripts,pkgTemp,pkgNumber,pkgPrice,pkgCook,pkgUnit) values('"] ;
    updateSql = [updateSql stringByAppendingFormat:@"%@','",AppSession.currentOrderMenu.momLocalMenu];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",AppSession.currentMyOrderDish.ppCode];//套餐代码
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppCode];//套餐明细代码
    
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppDescripts];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",self.ppTemp];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",numberString];
    
    updateSql = [updateSql stringByAppendingFormat:@"%@','",price];
    updateSql = [updateSql stringByAppendingFormat:@"%@','",@" "];
    updateSql = [updateSql stringByAppendingFormat:@"%@')",units];
    [DataBaseAccess Update:updateSql];
}

-(void)updatePluFood {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
         MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from PluFood where ppCode='%@'",ppCode]];
    PluFood *temp = [PluFood new];
    if ([rs next]) {
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.pfCooks = FormatNilObject([rs stringForColumn:@"pfCooks"]);
        temp.pfMeterials = FormatNilObject([rs stringForColumn:@"pfMeterials"]);
        temp.pfRemarks = FormatNilObject([rs stringForColumn:@"pfRemarks"]);
    }else {
        MyNSLog(@"have no result!");
    }
    [rs close];
    [db close];    
    self.ppFood = temp;
    [temp release];
}
-(void)updatePluCigarete {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from PluCigarete where ppCode='%@'",ppCode]];
    PluCigarete *temp = [PluCigarete new];
    if ([rs next]) {
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.pcNicotine = FormatNilObject([rs stringForColumn:@"pcNicotine"]);
        temp.pcPackages = FormatNilObject([rs stringForColumn:@"pcPackages"]);
        temp.pcTar = FormatNilObject([rs stringForColumn:@"pcTar"]);
        temp.pcCO = FormatNilObject([rs stringForColumn:@"pcCO"]);
        temp.pcRemarks = FormatNilObject([rs stringForColumn:@"pcRemarks"]);
    }
    [rs close];
    [db close];    
    self.ppCigarete = temp;
    [temp release];
}
-(void)updatePluDrink {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from PluDrink where ppCode='%@'",ppCode]];
    PluDrink *temp = [PluDrink new];
    if ([rs next]) {
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.pdCapacity = FormatNilObject([rs stringForColumn:@"pdCapacity"]);
        temp.pdPlaces = FormatNilObject([rs stringForColumn:@"pdPlaces"]);
        temp.pdPrecision = FormatNilObject([rs stringForColumn:@"pdPrecision"]);
        temp.pdTempereature = FormatNilObject([rs stringForColumn:@"pdTempereature"]);
        temp.pdYear = FormatNilObject([rs stringForColumn:@"pdYear"]);
        temp.pdRemarks = FormatNilObject([rs stringForColumn:@"pdRemarks"]);
    }
    [rs close];
    [db close];    
    self.ppDrink = temp;
    [temp release];
}
//获取套餐明细数组
-(void)updatePluPkg{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from PluPkg where ppCode='%@'",ppCode]];
    NSMutableArray *result=[NSMutableArray new];
    while ([rs next]) {
        PluPkg *temp = [PluPkg new];
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.pkgCode = FormatNilObject([rs stringForColumn:@"pkgCode"]);
        temp.pkgDescripts = FormatNilObject([rs stringForColumn:@"pkgDescripts"]);
        temp.pkgTemp = FormatNilObject([rs stringForColumn:@"pkgTemp"]);
        temp.pkgPrice = FormatNilObject([rs stringForColumn:@"pkgPrice"]);
        temp.pkgUnit = FormatNilObject([rs stringForColumn:@"pkgUnit"]);        
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    self.ppPluPkgs = result;
    [result release];
}
//初始化PluPlu对象时，要初始化属性：已点菜品(MyOrderDishes对象数组)
-(void)updateMyOrderDishes {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and ppCode='%@'",AppSession.currentOrderMenu.momLocalMenu, ppCode]];
    NSMutableArray *result=[NSMutableArray new];
    while ([rs next]) {
        MyOrderDishes *temp=[MyOrderDishes new];
        temp.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.modDescripts = FormatNilObject([rs stringForColumn:@"modDescripts"]);
        temp.modNumber = FormatNilObject([rs stringForColumn:@"modNumber"]);
        temp.modPrice = FormatNilObject([rs stringForColumn:@"modPrice"]);
        temp.modUnits = FormatNilObject([rs stringForColumn:@"modUnits"]);
        temp.modCook = FormatNilObject([rs stringForColumn:@"modCook"]);
        temp.modPriceUpdate = FormatNilObject([rs stringForColumn:@"modPriceUpdate"]);
        temp.modTime = FormatNilObject([rs stringForColumn:@"modTime"]);
        temp.modPluPlu = nil;
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    self.ppMyOrderDishes = result;
    [result release];
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc{    
//    MyNSLog(@"pluplu dealloc pmCode=%@,psCode=%@,ppCode=%@",self.pmCode,self.psCode,self.ppCode);
    [pmCode release];
    pmCode = nil;
    [psCode release];
    psCode = nil;
    [ppCode release];
    ppCode = nil;
    [ppTemp release];
    ppTemp = nil;
    [ppStatus release];
    ppStatus = nil;
    
    [ppColor release];
    ppColor = nil;
    [ppPosition release];
    ppPosition = nil;
    [ppHelpCode release];
    ppHelpCode = nil;
    [ppDescripts release];
    ppDescripts = nil;
    [ppImage release];
    ppImage = nil;
    [ppRecommended release];
    ppRecommended = nil;
    [ppSeqNormal release];
    ppSeqNormal = nil;
    [ppSeqCommend release];
    ppSeqCommend = nil;
    [ppType release];
    ppType = nil;
    [ppFood release];
    ppFood = nil;
    [ppCigarete release];
    ppCigarete = nil;
    [ppDrink release];
    ppDrink = nil;
    [ppPluPkgs release];
    ppPluPkgs = nil;
    
    [ppPluPrices release];
    ppPluPrices = nil;
    [ppMyOrderDishes release];
    ppMyOrderDishes = nil;
    
    [super dealloc];
}
@end
